﻿using Net.Event;
using Net.Server;
using Net.Share;
using System.Diagnostics;
using Cysharp.Threading.Tasks;
using S = Net.Server.WebServerNew;
using C = Net.Client.WebClient;

internal class Program
{
    static Stopwatch stopwatch;

    unsafe static void Main(string[] args)
    {
        Win32KernelAPI.timeBeginPeriod(1);

        NDebug.BindLogAll(Console.WriteLine);

        var program = new Program();

        //此处是服务器部分, 可以复制到另外一个控制台项目
        var server = new S();
        server.Config.LimitQueueCount = 10000000;//测试小数据的快速性能, 可以设置这里, 默认限制在65536
        server.Config.PackageLength = 10000000;//小数据包封包合包大小, 一次性能运送的小数据包数量
        server.Config.PackageSize = 1024 * 1024 * 50;//接收缓存数据包的最大值, 如果超出则被丢弃
        server.Config.SendBufferSize = 1024 * 1024 * 10; //发送窗口设置
        server.Config.ReceiveBufferSize = 1024 * 1024 * 10; //接收窗口设置
        server.Config.OnlineLimit = 10;
        server.Config.LineUp = 10;
        server.AddAdapter(new Net.Adapter.SerializeAdapter3());//采用极速序列化进行序列化rpc数据模型
        server.AddAdapter(new Net.Adapter.CallSiteRpcAdapter());//采用极速调用rpc方法适配器
        server.RpcAdapter.AddRpc(program);
        server.Run();

        //此处是客户端部分, 可以复制到另外一个控制台项目
        var client = new C();
        client.Config.LimitQueueCount = 10000000;
        client.Config.PackageLength = 10000000;
        client.Config.PackageSize = 1024 * 1024 * 50;
        client.Config.SendBufferSize = 1024 * 1024 * 10; //发送窗口设置
        client.Config.ReceiveBufferSize = 1024 * 1024 * 10; //接收窗口设置
        client.AddAdapter(new Net.Adapter.SerializeAdapter3());
        client.AddAdapter(new Net.Adapter.CallSiteRpcAdapter());
        client.RpcAdapter.AddRpc(program);
        client.Connect().AsTask().Wait();

        client.Call(new byte[1]);//先进入服务器
        Thread.Sleep(500);

        //client.Call(NetCmd.Local, new byte[1024 * 1024 * 3]);

        stopwatch = Stopwatch.StartNew();

        for (int i = 0; i < 1000000; i++)
        {
            //client.Call(NetCmd.Local, 2, i);
            //client.AddOperation(new Operation());
            client.Call(1, i);
            if (i % 10000 == 0)
                Thread.Sleep(1);
        }

        while (true)
        {
            var cmd = Console.ReadLine();
            if (cmd == "test") 
            {
                //client.Client.Close();
                client.WSClient.Close();
            }
            if (cmd == "test1")
            {
                client.Close();
            }
            if (cmd == "test2")
            {
                foreach (var clientPeer in server.Clients.Values)
                {
                    clientPeer.Close();
                }
            }
        }
    }

    [Rpc(NetCmd.SafeCallHash, 1)]
    void test1(ClientPeerBase client, int i)
    {
        client.Call(2, i);
    }

    [Rpc(2)]
    void test2(int i)
    {
        if (i % 10000 == 0)
            Console.WriteLine(i);
        if (i >= 999999)
        {
            stopwatch.Stop();
            Console.WriteLine(stopwatch.Elapsed);
        }
    }
}